package com.imooc.reader.management.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imooc.reader.management.dto.ResponseDTO;
import com.imooc.reader.management.entity.Member;
import com.imooc.reader.management.mapper.MemberMapper;
import com.imooc.reader.management.service.MemberService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/members")
public class MemberController {
    @Resource
    private MemberService memberService;

    @GetMapping("")
    public ResponseDTO list(Integer page, Integer rows, String mobile) {
        if (page == null) {
            page = 1;
        }
        if (rows == null) {
            rows = 10;
        }
        IPage<Member> list = memberService.selectByMobile(new Page(page, rows), mobile);
        return new ResponseDTO().put("list", list.getRecords()).put("count", list.getTotal());
    }

    @DeleteMapping("/{memberId}")
    public ResponseDTO deleteMember(@PathVariable("memberId") Long memberId) {
        Member member = memberService.deleteMember(memberId);
        return new ResponseDTO().put("member", member);
    }
}
